#!/usr/bin/env python
"""%prog [options] RUNFILE

Set up (and possibly run) the equilibration equilibrium simulation for one
compound and one solvent. All parameters except the solvent are specified in
the RUNFILE. A template RUNFILE can be generated with mdpow-get-runinput. See the
online documentation or the commented example file in
``doc/examples/benzene_runinput.cfg``. It is recommended to use absolute paths
to file names. The run input file uses Python's :mod:`ConfigParser`, which
describes the syntax of the file.

You will require:

  1. at least Gromacs 4.0.5 ready to run (check that all commands can
     be found)
  2. a structure file (PDB, GRO) for the compound
  3. a Gromacs ITP file for the compound (OPLS/AA force field)

"""

# TODO:
#
#  2) Default config file in user's home dir.

from mdpow.run import equilibrium_simulation
from mdpow.config import get_configuration

import logging
logger = logging.getLogger('mdpow')

if __name__ == "__main__":
    import sys
    import os.path
    from optparse import OptionParser

    parser = OptionParser(usage=__doc__)
    parser.add_option('--solvent', '-S', dest='solvent',
                      metavar="NAME",
                      help="solvent NAME for compound, 'water', 'octanol', 'cyclohexane' or 'toluene' [%default]")
    parser.add_option('--dirname', '-d', dest='dirname',
                      metavar="DIRECTORY",
                      help="generate files and directories in DIRECTORY, which is created "
                      "if it does not already exist. The default is to use the molecule "
                      "name from the run input file.")
    parser.add_option('--force', dest='force',
                      action="store_true",
                      help="XXX force re-reading all data [%default]")
    parser.set_defaults(solvent="water", dirname=None, force=False, permissive=False)
    opts,args = parser.parse_args()

    if len(args) == 0:
        logger.fatal("A run input file is required See --help.")
        sys.exit(1)

    runfile = args[0]

    if not os.path.exists(runfile):
        logger.fatal("Run input file %r not found...", runfile)
        sys.exit(1)

    cfg = get_configuration(runfile)
    S = equilibrium_simulation(cfg, opts.solvent, dirname=opts.dirname)
